//
//  DetailModleDB.m
//  Leisure
//
//  Created by lanou3g on 16/4/11.
//  Copyright © 2016年 wenze. All rights reserved.
//

#import "DetailModleDB.h"
#import "FMDB.h"
#import "DataBaseManager.h"

@interface DetailModleDB (){
    FMDatabase *dataBase;
}

@end


@implementation DetailModleDB

- (instancetype)init{
    if (self = [super init]) {
        // 绑定数据库
        dataBase = [DataBaseManager defaultManager].dataBase;
//        [dataBase open];
    }
    return self;
}

- (void)createTable{
    NSString *create = [NSString stringWithFormat:@"create table if not exists %@(title text,coverimg text,tingid text);",LoveListTable];
    BOOL isCreate = [dataBase executeUpdate:create];
    if (!isCreate) {
        NSLog(@"cteate table error");
    }else{
        NSLog(@"create table ok");
    }
}

- (void)insertModelWithModel:(DianTaiInfoModel *)model{
    NSString *insert = [NSString stringWithFormat:@"insert into %@(title,coverimg,tingid)values('%@','%@','%@');",LoveListTable,model.title,model.coverimg,model.tingid];
    [dataBase executeUpdate:insert];
}

- (NSArray *)selectModel{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    NSString *select = [NSString stringWithFormat:@"select * from %@;",LoveListTable];
    // 结果集
    FMResultSet *result = [dataBase executeQuery:select];
    while ([result next]) {
        // 对数据进行处理
        DianTaiInfoModel *model = [[DianTaiInfoModel alloc]init];
        model.title = [result stringForColumn:@"title"];
        model.tingid = [result stringForColumn:@"tingid"];
        model.coverimg = [result stringForColumn:@"coverimg"];
        [array addObject:model];
    }
    
    return array;
}

- (void)deleteModelWithTitle:(NSString *)title{
    NSString *delete = [NSString stringWithFormat:@"delete from %@ where title = '%@'",LoveListTable,title];
    [dataBase executeUpdate:delete];
}





@end
